const api = require('../../../../ws_micro_shop/utils/api.js');
const tools = require('../../../../ws_micro_shop/utils/getuserinfo.js');
const app = getApp();

Page({
  data: {
    unit: '',
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    let that = this;
    let oid;
    let scene = decodeURIComponent(options.scene);
    if (scene && scene !== 'undefined') {
      let scene_obj = scene.split(',');
      oid = scene_obj[0];
      app.ws_micro_shop_globalData.shopid = scene_obj[1];
      that.setData({
        oid,
      });
    }
    app.tabbarindex = options.tabbarindex ? options.tabbarindex - 0 : app.tabbarindex;
    let userinfo = wx.getStorageSync('user_info');
    Number(userinfo.info) === 0 && tools.onGetInfo(this, userinfo.auth_mode);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    app.pageOnLoad(this);
  },
  onShow() {
    this.loadOrderDetails();
  },
  loadOrderDetails() {
    let that = this;
    let oid = that.data.oid;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.group.order.clerk_order_details,
      data: {
        id: oid,
      },
      success: function (res) {
        if (res.code === 0) {
          if (Number(res.data.status) !== 3) {
            that.countDownRun(res.data.limit_time_ms);
          }
          that.setData({
            order_info: res.data,
            limit_time: res.data.limit_time,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg || '',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                app.util.navigateTo('/ws_micro_shop_pt/pages/pt/order/order', {}, true);
              }
            },
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      },
    });
  },
  copyText(e) {
    let text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: text,
      success: function () {
        wx.showToast({
          title: "已复制",
          icon: "none"
        });
      },
    });
  },
  clerkOrder(e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认核销？',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '正在加载',
          });
          app.ws_micro_shoprequest({
            url: api.group.order.clerk,
            data: {
              order_id: that.data.order_info.order_id,
            },
            success: function (res) {
              if (res.code === 0) {
                wx.showModal({
                  showCancel: false,
                  content: '核销成功',
                  confirmText: '确认',
                  success: function (res) {
                    if (res.confirm) {
                      app.util.navigateTo('/ws_micro_shop/pages/user/user', {}, true);
                    }
                  },
                });
              } else {
                wx.showModal({
                  title: '温馨提醒',
                  showCancel: false,
                  content: res.msg || '',
                  confirmText: '确认',
                  success: function (res) {
                    if (res.confirm) {
                      app.util.navigateTo('/ws_micro_shop/pages/index/index', {}, true);
                    }
                  },
                });
              }
            },
            complete: function () {
              wx.hideLoading();
            },
          });
        }
      },
    });
  },
  location() {
    let shop = this.data.order_info.shop;
    wx.openLocation({
      latitude: parseFloat(shop.latitude),
      longitude: parseFloat(shop.longitude),
      address: shop.address,
      name: shop.name,
    });
  },
});
